/**
 * \brief  Fiasco  Framebuffer driver
 * \author Sebastian Sumpf
 * \author Christian Helmuth
 * \date   2007-09-11
 */

/*
 * Copyright (C) 2007-2009 Sebastian Sumpf
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#include <base/env.h>
#include <base/stdint.h>
#include <base/printf.h>
#include <base/snprintf.h>
#include <io_mem_session/connection.h>

#include "framebuffer.h"
#include "vesa.h"

using namespace Genode;
using namespace Vesa;

#include "vbe.h"

/**
 * Framebuffer area
 */
static Dataspace_capability io_mem_cap;


/****************
 ** Driver API **
 ****************/

Dataspace_capability Framebuffer_drv::hw_framebuffer()
{
	return io_mem_cap;
}


int Framebuffer_drv::map_io_mem(addr_t base, size_t size, void **out_addr,
                                addr_t addr, Dataspace_capability *out_io_ds)
{
	Io_mem_connection io_mem(base, size);
	io_mem.on_destruction(Io_mem_connection::KEEP_OPEN);
	Io_mem_dataspace_capability io_ds = io_mem.dataspace();
	if (!io_ds.valid())
		return -2;

	try { *out_addr = env()->rm_session()->attach(io_ds, size, 0, addr != 0, addr); }
	catch (Rm_session::Attach_failed e) { return -3; }

	if(out_io_ds)
		*out_io_ds = io_ds;

	return 0;
}


int Framebuffer_drv::set_mode(unsigned long width, unsigned long height,
                              unsigned long mode)
{
	mb_vbe_ctrl_t *ctrl_info;
	mb_vbe_mode_t *mode_info;
	char * oem_string;
	uint16_t vesa_mode;
	uint32_t phys;
	uint16_t ctrl_offs = 0x100, mode_offs = 0x800; /* static */


	/* set location of data types */
	ctrl_info = reinterpret_cast<mb_vbe_ctrl_t*>(X86emu::x86_mem.data_addr()
	                                             + ctrl_offs);
	mode_info = reinterpret_cast<mb_vbe_mode_t*>(X86emu::x86_mem.data_addr()
	                                             + mode_offs);

	/* retrieve vesa mode hex value */
	if (!(vesa_mode = get_vesa_mode(width, height, mode))) {
		PWRN("graphics mode %lux%lu@%lu not found", width, height, mode);
		return -1;
	}

	/* use current refresh rate, set flat framebuffer model */
	vesa_mode = (vesa_mode  & VBE_CUR_REFRESH_MASK) | VBE_SET_FLAT_FB;

	/* retrieve controler information */
	if (X86emu::x86emu_cmd(VBE_CONTROL_FUNC, 0, 0, ctrl_offs) != VBE_SUPPORTED)
	{
		PWRN("VBE Bios not present");
		return -2;
	}

	/* determine VBE version and OEM string */
	phys = (ctrl_info->oem_string & 0xFFFF) +
	       ((ctrl_info->oem_string >> 12) & 0xFFFF0);

	oem_string = X86emu::virt_addr<char*>(phys);

	printf("Found: VESA BIOS version %d.%d\nOEM: %s\n",
	       ctrl_info->version >> 8, ctrl_info->version & 0xFF,
	       ctrl_info->oem_string ? oem_string : "[unknown]");

	if (ctrl_info->version < 0x200) {
		PWRN("VESA Bios version 2.0 or later required");
		return -3;
	}

	/* get mode info */
	/* 0x91 tests MODE SUPPORTED (0x1) | GRAPHICS  MODE (0x10) | LINEAR
	 * FRAME BUFFER (0x80) bits */
	if (X86emu::x86emu_cmd(VBE_INFO_FUNC, 0, vesa_mode, mode_offs) != VBE_SUPPORTED
	   || (mode_info->mode_attributes & 0x91) != 0x91) {
		PWRN("graphics mode %lux%lu@%lu not supported", width, height, mode);
		return -4;
	}

	/* set mode */
	if ((X86emu::x86emu_cmd(VBE_MODE_FUNC, vesa_mode) & 0xFF00) != VBE_SUCCESS) {
		PDBG("VBE SET error");
		return -5;
	}

	/* map framebuffer */
	if (!io_mem_cap.valid()) {
		X86emu::x86emu_cmd(VBE_INFO_FUNC, 0, vesa_mode, mode_offs);

		PDBG("FB phys at 0x%08x size: 0%08x", mode_info->phys_base,
		     ctrl_info->total_memory << 16);
		void *fb;
		map_io_mem(mode_info->phys_base, ctrl_info->total_memory << 16, &fb, 0,
		           &io_mem_cap);
	}

	/* verbose output */
	X86emu::verbose();

	return 0;
}


/********************
 ** Driver startup **
 ********************/

int Framebuffer_drv::init()
{
	if (X86emu::init())
		return -1;

	return 0;
}
